package com.jz.community.modulemine.ui.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.utils.DownloadAppManager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EnterMerchantActivity extends BaseMvpActivity {

    @BindView(R2.id.enter_merchant_back)
    ImageView enterMerchantBack;
    @BindView(R2.id.enter_merchant_title)
    TextView enterMerchantTitle;
    @BindView(R2.id.enter_merchant_toolbar)
    Toolbar enterMerchantToolbar;
    @BindView(R2.id.enter_merchant_img)
    ImageView enterMerchantImg;
    @BindView(R2.id.enter_merchant_btn_pic)
    ImageView enterMerchantBtnPic;
    @BindView(R2.id.enter_merchant_btn_tv)
    TextView enterMerchantBtnTv;
    @BindView(R2.id.enter_merchant_btn)
    RelativeLayout enterMerchantBtn;
    @BindView(R2.id.enter_merchant_ll)
    RelativeLayout enterMerchantLl;

    final static String PackageName_MERCHANT = "cn.shequren.merchant";//商家端

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_mine_activity_enter_merchant;
    }

    @Override
    protected void initView() {
        setImmersionBar(enterMerchantToolbar);
        enterMerchantTitle.setText("商家入驻");
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    private boolean isAppInstalled(String pckName) {
        if (Preconditions.isNullOrEmpty(pckName)) {
            return false;
        }
        List<PackageInfo> installedApps = getPackageManager().getInstalledPackages(0);
        if (installedApps != null && !installedApps.isEmpty()) {
            for (PackageInfo pckInfo : installedApps) {
                if (pckInfo.packageName.equals(pckName)) {
                    return true;
                }
            }
        }
        return false;
    }

    private void startApp(String pkgName) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> mainActivity = getPackageManager().queryIntentActivities(intent, 0);
        if (mainActivity != null && !mainActivity.isEmpty()) {
            for (ResolveInfo activityInfo : mainActivity) {
                if (activityInfo.activityInfo.exported && activityInfo.activityInfo.packageName.equals(pkgName)) {
                    intent.setClassName(activityInfo.activityInfo.packageName, activityInfo.activityInfo.name);
                    startActivity(intent);
                }
            }
        }
    }

    private void hintNotApp(final int pid) {
        String name = "";
        switch (pid) {
            case 2:
                name = getString(R.string.shequren_merchant);
                break;
        }

        new MaterialDialog.Builder(this)
                .title("提示")
                .content(getString(R.string.is_not_installed_app, name))
                .positiveText("确认")
                .negativeText("取消")
                .negativeColor(ContextCompat.getColor(this,R.color.negative_color))
                .positiveColor(ContextCompat.getColor(this,R.color.positive_color))
                .onPositive((dialog, which) -> {
                    DownloadAppManager manager = new DownloadAppManager(EnterMerchantActivity.this,
                            "merchant.apk", "http://www.shequren.cn/index.php?m=Api&c=App&a=down_app&pid=2");
                    manager.showDownloadDialog();
                    dialog.dismiss();
                })
                .onNegative((dialog, which) -> dialog.dismiss())
                .show();
    }

    @OnClick({R2.id.enter_merchant_back, R2.id.enter_merchant_btn})
    public void onClick(View view) {

        if (view.getId() == R.id.enter_merchant_back) {
            finish();
        }

        if (view.getId() == R.id.enter_merchant_btn) {
            if (isAppInstalled(PackageName_MERCHANT)) {
                //已安装商家端APP 直接启动
                startApp(PackageName_MERCHANT);
            } else {
                //未安装商家端APP 提示安装
                hintNotApp(2);
            }
        }
    }
}
